package com.ken.checkviolationapp.ui.dashboard

import android.os.Bundle
import android.text.Editable
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.ken.base_http_library.ServiceCreator
import com.ken.base_http_library.ServiceCreator.Companion.JUHE_GNYJ_BASE_URL
import com.ken.base_http_library.http_api_service.JuHeApiService
import com.ken.base_http_library.oil_price.OilPriceResponse
import com.ken.base_http_library.response.JuHeBaseResponse
import com.ken.base_library.ui.BaseFragment
import com.ken.base_library.widget.MySearchView
import com.ken.checkviolationapp.R
import com.ken.checkviolationapp.adapter.OilPriceAdapter
import com.ken.checkviolationapp.bean.EmptyData
import com.ken.checkviolationapp.databinding.FragmentDashboardBinding
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.observers.DisposableObserver
import io.reactivex.schedulers.Schedulers
import kotlinx.android.synthetic.main.include_empty_data.view.*

class DashboardFragment : BaseFragment() {
    val TAG = DashboardFragment::class.java.name
    private lateinit var dashboardViewModel: DashboardViewModel
    lateinit var adapter: OilPriceAdapter
    lateinit var fragmentDashboardBinding: FragmentDashboardBinding
    var mList: MutableList<OilPriceResponse> = arrayListOf()
    var allList: MutableList<OilPriceResponse> = arrayListOf()

    override fun initDataBind(inflater: LayoutInflater) {
        fragmentDashboardBinding = FragmentDashboardBinding.inflate(inflater)
    }

    override fun initView(view: View?, savedInstanceState: Bundle?) {
        setAdapter()
        fragmentDashboardBinding.pbLoading.visibility = View.VISIBLE
        fragmentDashboardBinding.data = EmptyData("无数据...")
        getOilPriceToday()
    }

    override fun initListener() {
        fragmentDashboardBinding.mySearchView.addSearchViewTextChange(object :
            MySearchView.OnSearchAfterTextChange {
            override fun beforeTextChanged(charSequence: CharSequence?, i: Int, i1: Int, i2: Int) {
                println("beforeTextChanged...")
            }

            override fun onTextChanged(charSequence: CharSequence?, i: Int, i1: Int, i2: Int) {
                println("onTextChanged...")
            }

            override fun onAfterTextChanged(editable: Editable?) {
                println("onAfterTextChanged...")
                mList.clear()
                for (oil in allList) {
                    if (oil.city.contains(editable.toString().trim())) {
                        mList.add(oil)
                    }
                }
                if (mList.size == 0) {
                    fragmentDashboardBinding.root.llEmptyData.visibility = View.VISIBLE
                } else {
                    fragmentDashboardBinding.root.llEmptyData.visibility = View.GONE
                }
                adapter.notifyDataSetChanged()
            }

        })
    }

    override fun initData() {


    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_dashboard
    }


    /**
     * 获取今日油价
     * */
    private fun getOilPriceToday() {
        Log.e(TAG, "获取今日油价")
        val service = ServiceCreator.mInstance.get(JUHE_GNYJ_BASE_URL).create(JuHeApiService::class.java)
        val observer1 = service.getTodayOilPrice("2ffa84a5e1d7176835dbafc83f97a648")

        observer1.subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : DisposableObserver<Any>() {
                override fun onComplete() {
                    fragmentDashboardBinding.pbLoading.visibility = View.GONE
                    Log.e(TAG, "完成")
                }

                override fun onNext(t: Any) {
                    if (t is JuHeBaseResponse<*>) {

                        Log.e(TAG, "下一步")
                        println("code..${t.error_code}" + "result...${t.reason}")

                        mList.addAll(t.result as List<OilPriceResponse>)
                        allList.addAll(mList)
                        adapter.notifyDataSetChanged()
                        if (mList.size == 0) {
                            fragmentDashboardBinding.root.llEmptyData.visibility = View.VISIBLE
                        } else {
                            fragmentDashboardBinding.root.llEmptyData.visibility = View.GONE
                        }
                    }
                }

                override fun onError(e: Throwable) {
                    fragmentDashboardBinding.pbLoading.visibility = View.GONE
                    Log.e(TAG, "异常")
                    println("message..${e.message}")
                }
            })
    }


    private fun setAdapter() {
        adapter = context?.let { OilPriceAdapter(it, 0, mList) }!!
        fragmentDashboardBinding.rvList.layoutManager = LinearLayoutManager(context)
        fragmentDashboardBinding.rvList.adapter = adapter

    }

    override fun databing(): View {
        return fragmentDashboardBinding.root
    }


}
